package saxion.speelveld.spaceengineer.model;

import java.io.Serializable;
import java.util.ArrayList;

public class ResourceWrapper implements Serializable
{
	private Resource[] resources;

	public ResourceWrapper()
	{
		resources = new Resource[ResourceType.values().length];
	}

	public void addResourceAmount(ResourceType type, int amount)
	{
		boolean found = false;
		int pos;
		for (pos = 0; pos < resources.length; pos++)
		{
			if (resources[pos] != null && resources[pos].getResourceType() == type)
			{
				found = true;

			}
			if (found)
			{
				resources[pos].setAmount(resources[pos].getAmount() + amount);
				break;
			}
		}
		if (!found)
		{
			for (pos = 0; pos < resources.length; pos++)
			{
				if (resources[pos] == null)
				{
					break;
				}
			}
			resources[pos] = new Resource(type, amount);
		}
	}

	public ResourceWrapper(Resource[] resources)
	{
		this.resources = resources;
	}

	public Resource[] getResourcesArray()
	{
		return resources;
	}

	public ArrayList<Resource> getResourcesArrayList()
	{
		ArrayList<Resource> resourceArrayList = new ArrayList<Resource>();
		for (Resource r : resources)
		{
			// Log.d("hoi", r.getResourceType() + " " + r.getAmount());
			resourceArrayList.add(r);
		}
		return resourceArrayList;
	}

	public int getResourceAmount(ResourceType type)
	{
		// for (Resource r : resources)
		// {
		// Log.d("hoi", r.getResourceType() + " " + r.getAmount());
		// }
		return resources[type.ordinal()].getAmount();
	}
}
